﻿using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Utility.Extensions;

namespace Shared.SqlAuditHelper
{
    /// <summary>
    /// SQL 审计日志
    /// http://www.codeisbug.com/Doc/8/1140
    /// </summary>
    public static class SqlAudit
    {
        /// <summary>
        /// 数据库操作前和操作后的数据变化
        /// </summary>
        public static Action<DiffLogModel> DiffLogEvent { set; get; }
        //    = (it) =>
        //{
        //    var editBeforeData = it.BeforeData;
        //    var editAfterData = it.AfterData;
        //    var sql = it.Sql;
        //    var parameter = it.Parameters;
        //    var data = it.BusinessData;
        //    var time = it.Time;
        //    var diffType = it.DiffType;//枚举值 insert 、update 和 delete 用来作业务区分

        //    //你可以在这里面写日志方法

        //};
        /// <summary>
        /// 执行SQL 错误事件
        /// exp.sql exp.parameters 可以拿到参数和错误Sql  
        /// </summary>
        public static Action<SqlSugarException> Error { set; get; }
            = (exp) =>
        {

            //exp.sql exp.parameters 可以拿到参数和错误Sql             

        };
        /// <summary>
        /// SQL执行前事件
        /// </summary>
        public static Action<string, SugarParameter[]> LogExecuting { set; get; }
            = (sql, pars) =>
        {
            Console.WriteLine($"T-SQL: {sql}\r\nParam:  {pars.ToDictionary(it => it.ParameterName, it => it.Value).ToJsonString()}");
        };                      
        /// <summary>
        /// SQL执行完事件
        /// </summary>
        public static Action<string, SugarParameter[]> LogExecuted { set; get; }
            = (sql, pars) =>
        {

        };
        /// <summary>
        /// SQL执行前 可以修改SQL
        /// </summary>
        public static Func<string, SugarParameter[], KeyValuePair<string, SugarParameter[]>> ExecutingChangeSql { set; get; }
        //    = (sql, pars) =>
        //{
        //    return new KeyValuePair<string, SugarParameter[]>(sql, pars);
        //};
    }
}
